package com.blueline.sms.webservice;

import com.blueline.sms.webservice.impl.SenderImpl;
import org.apache.cxf.configuration.jsse.TLSServerParameters;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.xml.ws.Endpoint;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

public class WebService {


    private static final int httpsPort = 18889;
    private static final int httpPort = 18888;


    private static final String httpsAddress = "https://0.0.0.0:"+ httpsPort +"/webservice.asmx";
    private static final String httpAddress = "http://0.0.0.0:"+ httpPort +"/webservice.asmx";

    public static void main(String[] args) throws Exception {



        configureSSLOnTheServer(new File("httpsblueline.jks"),"1qaz@WSX","1qaz@WSX");
        SenderImpl senderService=new SenderImpl();
        Endpoint httpsServer = javax.xml.ws.Endpoint.publish(httpsAddress, senderService);
        Endpoint httpServer = javax.xml.ws.Endpoint.publish(httpAddress, senderService);
        System.out.println("web service is ok.");


    }

    public static void configureSSLOnTheServer(File jks,String password ,String storePassword) {

        try {
            TLSServerParameters tlsParams = new TLSServerParameters();
            KeyStore keyStore = KeyStore.getInstance("JKS");

            keyStore.load(new FileInputStream(jks), storePassword.toCharArray());
            KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyFactory.init(keyStore, password.toCharArray());
            KeyManager[] keyManagers = keyFactory.getKeyManagers();
            tlsParams.setKeyManagers(keyManagers);

            keyStore.load(new FileInputStream(jks), storePassword.toCharArray());
            TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustFactory.init(keyStore);
            TrustManager[] trustManagers = trustFactory.getTrustManagers();
            tlsParams.setTrustManagers(trustManagers);

//            FiltersType filtersTypes = new FiltersType();
//            filtersTypes.getInclude().add(".*_EXPORT_.*");
//            filtersTypes.getInclude().add(".*_EXPORT1024_.*");
//            filtersTypes.getInclude().add(".*_WITH_DES_.*");
//            filtersTypes.getInclude().add(".*_WITH_NULL_.*");
//            filtersTypes.getExclude().add(".*_DH_anon_.*");
//            tlsParams.setCipherSuitesFilter(filtersTypes);

//            ClientAuthentication ca = new ClientAuthentication();
//            ca.setRequired(true);
//            ca.setWant(true);
//            tlsParams.setClientAuthentication(ca);

            JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory();
            factory.setTLSServerParametersForPort(httpsPort, tlsParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
